﻿using System;
using nRoute.Utilities;

namespace nRoute.Components.Composition
{
    public class WeakReferenceResourceLocator
        : ResourceLocatorBase
    {
        private WeakReference _resourceReference;

        public WeakReferenceResourceLocator(Object resource)
        {
            Guard.ArgumentNotNull(resource, "resource");
            _resourceReference = new WeakReference(resource, false);
        }

        protected override object GetInstance(ITypeBuilder typeBuilder)
        {
            // automatically returns null if the resource has been collected
            return _resourceReference.Target;
        }
    }
}
